DZApp.service('activityRuleService', function(commonService) {
	return {
		showList : function() {
			var url = "/operationService/listActivityRule";
			return commonService.commonAjaxByPost(url);
		},
		showSeviceSelect : function() {
			var url = "/operationService/listLevel2OrderServiceInfo";
			return commonService.commonAjaxByPost(url);
		},
		showMerchantSelect : function() {
			var url = "/operationService/listMerchant";
			return commonService.commonAjaxByPost(url);
		},
		editData : function(postData) {
			var url = "/operationService/saveOrUpdateActivityRule";
			return commonService.commonAjaxByPost(url, postData);
		},
		delData : function(postData) {
			var url = "/operationService/deleteActivityRule";
			return commonService.commonAjaxByPost(url, postData);
		},
		validate : function($scope) {
			var startDate = $scope.edit.startDate;
			var endDate = $scope.edit.endDate;
			if (!startDate || !endDate) {
				return true;
			}
			var sD = new Date(startDate).getTime();
			var eD = new Date(endDate).getTime();
			if (sD > eD) {
				bootboxWindow.alert('开始时间必须小于结束时间', null);
				return false;
			}
			
			var datetimeRgx = /^[1-9](\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\s([0-1](\d{1})|2[0-3])[:][0-5](\d{1})$/;
			if(!startDate.match(datetimeRgx)){
				bootboxWindow.alert('开始时间格式不正确！', null);
				return false;
			}
			if(!endDate.match(datetimeRgx)){
				bootboxWindow.alert('结束时间格式不正确！', null);
				return false;
			}
			return true;
		}
	};
});

DZApp.controller('activityRuleController', function($scope,
		activityRuleService, $state) {
	activityRuleService.showList().then(function(data) {
		$scope.showList = eval(data.bizData);
	});
	activityRuleService.showSeviceSelect().then(function(data) {
		$scope.showSeviceSelect = eval(data.bizData);
	});
	activityRuleService.showMerchantSelect().then(function(data) {
		$scope.showMerchantSelect = eval(data.bizData);
	});
	$scope.theObj = null;
	$scope.editData = function() {
		if (!bValidateObj.bValidateCall($('#editData'))) {
			return false;
		}
		if (!activityRuleService.validate($scope)) {
			return false;
		}
		var postData = $.param({
			activityRuleStr : JSON.stringify($scope.edit),
			saveOrUpdate : ($scope.openTitle == '新增' ? "C" : "U")
		});
		activityRuleService.editData(postData).then(function(data) {
			if (data.retCode == 'success') {
				if ($scope.openTitle == '新增') {
					$state.reload();
				} else {
					$('#editData').modal('hide');
					for ( var p in $scope.theObj) {
						$scope.theObj[p] = $scope.edit[p];
					}
					$state.reload();
				}
			} else {
				bootboxWindow.alert('保存失败，' + data.errorMsg, null);
			}
		});
	};
	$scope.addInit = function() {
		$scope.openTitle = '新增';
		$scope.edit = {
			serviceNo : '',
			serviceProviderCode : '',
			ruleName : '',
			salience : '',
			startDate : '',
			endDate : '',
			conditionalExpression : '',
			bizExpression : ''
		};
	};
	$scope.setInit = function(_obj) {
		$scope.theObj = _obj;
		$scope.openTitle = '修改';
		$scope.edit = angular.copy(_obj);
	};
	$scope.delInit = function(_obj) {
		bootboxWindow.confirm('确定要删除活动规则？', function() {
			var postData = $.param({
				serviceNo : _obj.serviceNo,
				serviceProviderCode : _obj.serviceProviderCode
			});
			activityRuleService.delData(postData).then(function(data) {
				if (data.retCode == 'success') {
					$state.reload();
				} else {
					bootboxWindow.alert('删除失败，请重试', null);
				}
			});
		}, null);
	};
});